using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using Crosland.Framework.DataHelper;
using Crosland.Framework.Base;
using Crosland.Offline.Core;
using Crosland.Offline.Data.Helper;

namespace Crosland.Offline.Data.Dao
{
    public class UsuarioRolDao : DaoBase
    {
        public UsuarioRolDao(string strConn) : base(strConn)
        {
        }

        UsuarioRolHelper helper = new UsuarioRolHelper();

        public int Save(UsuarioRol entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpSave);

            dbProvider.AddInParameter(command, helper.IdRol, DbType.Int32, entity.ID.IdRol);
            dbProvider.AddInParameter(command, helper.IdUsuario, DbType.String, entity.ID.IdUsuario);

            return dbProvider.ExecuteNonQuery(command);
        }

        public void Update(UsuarioRol entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpUpdate);

            dbProvider.AddInParameter(command, helper.IdRol, DbType.Int32, entity.ID.IdRol);
            dbProvider.AddInParameter(command, helper.IdUsuario, DbType.String, entity.ID.IdUsuario);

            dbProvider.ExecuteNonQuery(command);
        }

        public void Delete(UsuarioRol.UsuarioRolID id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpDelete);

            dbProvider.AddInParameter(command, helper.IdRol, DbType.Int32, id.IdRol);
            dbProvider.AddInParameter(command, helper.IdUsuario, DbType.String, id.IdUsuario);

            dbProvider.ExecuteNonQuery(command);
        }

        public UsuarioRol GetById(UsuarioRol.UsuarioRolID id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetById);

            dbProvider.AddInParameter(command, helper.IdRol, DbType.Int32, id.IdRol);
            dbProvider.AddInParameter(command, helper.IdUsuario, DbType.String, id.IdUsuario);
            UsuarioRol entity = null;

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    entity = helper.Create(dr);
                }
            }

            return entity;
        }

        public List<UsuarioRol> List()
        {
            List<UsuarioRol> entitys = new List<UsuarioRol>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpList);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public List<UsuarioRol> GetByCriteria(string userName)
        {
            List<UsuarioRol> entitys = new List<UsuarioRol>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetByCriteria);
            dbProvider.AddInParameter(command, helper.IdUsuario, DbType.String, userName);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }
    }
}
